﻿YUI.add('agregar-calendarizaciones-view', function (Y) {
    'use strict';
    Y.EditarCalendarizacion = Y.Base.create('EditarCalendarizacion', Y.View, [], {
        templateIsCharged: false,
        loadTemplate: function () {
            if (this.template == '') {
                var that = this,
                validator = null,
                request = Y.io('/ExcellentProject/scripts/views/examenes/calendarizaciones/templates/calendarizaciones.html', {
                    on: {
                        complete: function (id, response) {
                            that.templateIsCharged = true;
                            that.template = response.responseText;
                            that.render();
                        }
                    },
                    failure: function (id, response) {
                        alert(response)
                    }
                })
            }
        },
        events: {
            '.guardar': {
                click: 'guardarCalendarizacion'
            }
        },
        render: function () {
            debugger
            this.loadTemplate();
            if (this.templateIsCharged) {
                var contentTemplate = Y.Handlebars.compile(this.template);
                this.get('container').setHTML(contentTemplate(Y.merge(this.get('model').getAttrs(), this.get('presentationModel').getAttrs())));
                var statusList = document.getElementById('estado');
                statusList.options[statusList.options.length] = new Option('Activo', 'Activo');
                statusList.options[statusList.options.length] = new Option('Inactivo', 'Inactivo');
                $.fn.datetimepicker.defaults = {
                    pickDate: true,                 //en/disables the date picker
                    pickTime: true,                 //en/disables the time picker
                    useMinutes: true,               //en/disables the minutes picker
                    useSeconds: true,               //en/disables the seconds picker
                    useCurrent: true,               //when true, picker will set the value to the current date/time     
                    minuteStepping: 1,               //set the minute stepping
                    minDate: '1/1/1900',               //set a minimum date
                    maxDate: '1/1/2050',   //set a maximum date (defaults to today +100 years)
                    showToday: true,                 //shows the today indicator
                    language: 'en',                  //sets language locale
                    defaultDate: "",                 //sets a default date, accepts js dates, strings and moment objects
                    disabledDates: [],               //an array of dates that cannot be selected
                    enabledDates: [],                //an array of dates that can be selected
                    icons: {
                        time: 'glyphicon glyphicon-time',
                        date: 'glyphicon glyphicon-calendar',
                        up: 'glyphicon glyphicon-chevron-up',
                        down: 'glyphicon glyphicon-chevron-down'
                    },
                    useStrict: false,               //use "strict" when validating dates  
                    sideBySide: false,              //show the date and time picker side by side
                    daysOfWeekDisabled: []          //for example use daysOfWeekDisabled: [0,6] to disable weekends 
                };
                $('#fechainicio').datetimepicker();
                $('#fechafin').datetimepicker();
            }
        },
        guardarCalendarizacion: function (ev) {
            var model = this.get('model')
            debugger
            model.setAttrs({
                Descripcion: Y.one('#descripcion').get('value'),
                Intentos: Y.one('#intentos').get('value'),
                Fecha: Y.one('#fechainicio').get('value'),
                TiempoExamen: Y.one('#tiempoexamen').get('value'),
                FechaCierre: Y.one('#fechafin').get('value'),
                Estado: Y.one('#estado').get('value'),
                IdCuestionario: Y.one('#cuestionarios').get('value')
            });
            var options = null;

            if ((model.get('IdCalendarizacion') != 0) && (model.get('IdCuestionario')))
                options = { action: 'update', idCalen: model.get('IdCalendarizacion') };
            else
                options = { idCalen: '' }
            var that = this, a = model.save(options, function (err, response) {
                if (err != null)
                    alert('Ocurrieron problemas para actualizar la calendarizacion' + err)
                else {
                    var data = Y.JSON.parse(response.responseText);
                    model.setAttrs({
                        IdCalendarizacion: data.IdCalendarizacion,
                        Descripcion: data.Descripcion,
                        Intentos: data.Intentos,
                        Fecha: data.Fecha,
                        TiempoExamen: data.TiempoExamen,
                        FechaCierre: data.FechaCierre,
                        Estado: data.Estado,
                    });
                    alert('Calendarizacion Creada');

                    //saveCalendarizacion
                    Y.one('#saveCalendarizacion').set('href', '/calendarizacionescuestionario/scheduler/' + data.IdCalendarizacion + '/' + data.IdCuestionario);
                    Y.one('#saveCalendarizacion').simulate('click');
                }
            });
            //delay de 2 segundos para evitar lectura sucia
            //setTimeout(null, 2000);
        }

    })
}, '1.0.0.0', { requires: ['handlebars', 'handlebars-select-helper', 'node', 'aui-form-validator', 'aui-scheduler', 'event', 'node-event-simulate'] })